﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.function
{
    public partial class search : PageBase
    {
      //  public string tab = "sys_user";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["tab"]))
                    ViewState["tab"] = Request["tab"].Trim(); 
                else
                    ViewState["tab"] = "sys_user";

                fill();
                BLL.pub.pubwhere.del_where(Session["ID"].ToString(), ViewState["tab"].ToString());
            }
        }

        private void fill()
        {
            
            DataTable d = BLL.BLLfun.get_search(ViewState["tab"].ToString());
            ddlfiled.Items.Clear();
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            }
            ddlfiled.DataBind();


            DataTable e = BLL.BLLfun.get_where();
            ddlwhere.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            }
             ddlwhere.DataBind();   ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));
            ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));

            DataTable dt = BLL.pub.pubwhere.get_where(Session["ID"].ToString(), ViewState["tab"].ToString());
            Grid1.DataSource = dt;
            Grid1.DataBind();
        }

     
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            TTB.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = TTB.Text;
          
          //  if (ddlw.Equals("="))
           //     whe = "" + ddlf + " like  '%" + key + "%'";
          //  else
           //     whe = "" + ddlf + " " + ddlw + " '" + key + "'";
          //  if (key == "") whe = "";

           // fill();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            TTB.Text = String.Empty;
            TTB.ShowTrigger1 = false;
            fill();
        }

        protected void btsearch_Click(object sender, EventArgs e)
        {
            string txt = ddlfiled.SelectedText + ddlwhere.SelectedText + TTB.Text;
            string val = ddlfiled.SelectedValue + ddlwhere.SelectedValue +"''"+ TTB.Text+"''";
          
         
             if (ddlwhere.SelectedValue=="=")
                      val = ddlfiled.SelectedValue + " like " + "''%" + TTB.Text + "%''";


            BLL.pub.pubwhere.ins_where(Session["ID"].ToString(), ViewState["tab"].ToString(), val, txt);
            
            fill();

        }

        protected void btclose_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            HY.BLL.HR.sys_user sd = new BLL.HR.sys_user();
      
            StringBuilder sb = new StringBuilder();
        
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
               sb.Append(Grid1.DataKeys[row][0].ToString());
               sb.Append(",");
               }

         
            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));

            BLL.pub.pubwhere.del_where(sb.ToString().TrimEnd(','));                     

            fill();
        }

    }
}